package com.fudan.sys.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fudan.framework.base.DaoService;
import com.fudan.framework.entity.TableJson;
import com.fudan.framework.utils.EntityUtils;
import com.fudan.framework.web.controller.AddController;
import com.fudan.framework.web.controller.ListController;
import com.fudan.framework.web.controller.PathController;
import com.fudan.sys.entity.User;
import com.fudan.sys.security.PasswordUtils;
import com.fudan.sys.service.UserService;

@Controller
@RequestMapping("/sys/user")
public class UserController implements PathController,ListController<User>,AddController<User> {
	
	@Autowired
	private UserService userService;
	
	@Override
	public String pathPrefix() {
		return "sys/user";
	}
	public TableJson list(HttpServletRequest request) {
		int page =  Integer.valueOf(request.getParameter("page"));
		int size = Integer.valueOf(request.getParameter("limit"));
		TableJson json= TableJson.build(userService.getUserList(page,size), userService.countAll()) ;
		return json;
	}
	@Override
	public DaoService<User> injectService() {
		return userService;
	}

	@Override
	public User buildEntity(HttpServletRequest request) throws Exception {
		User user = EntityUtils.toEntity(request, User.class);
		user.setPassword(PasswordUtils.encrypt("123321"));
		return user;
	}
}
